<template>
	<view class="page">
		<view class="update-form">
			<view class="form-item">
				<view class="form-title">
					旧&nbsp;密&nbsp;码&nbsp;:
				</view>
				<input type="password" placeholder="请输入旧密码" maxlength="11"auto-focus v-model="oldPwd"/>
			</view>
			<view class="form-item">
				<view class="form-title">
					新&nbsp;密&nbsp;码&nbsp;:
				</view>
				<input type="password" placeholder="请输入新密码"  maxlength="11"v-model="newPwd"/>
			</view>
			<view class="form-item">
				<view class="form-title">
					确认密码&nbsp;:
				</view>
				<input type="password" placeholder="请确认密码"  maxlength="11" v-model="confirmPassword"/>
			</view>
		</view>
		<view class="submit-btn" @click="confirm">
			确定
		</view>
	</view>
</template>

<script>
	import { updatePassword } from '../../api/index.js';
	export default {
		data() {
			return {
				oldPwd:"",
				newPwd:"",
				confirmPassword:""
			}
		},
		methods: {
			confirm(){
				if(!this.oldPwd.trim() || !this.newPwd.trim() || !this.confirmPassword.trim()){
					uni.showToast({
						icon:'error',
						title:"输入不能为空"
					})
					return;
				}
				if(this.newPwd === this.confirmPassword){
					//组合数据把旧密码和新密码发送后端
					let obj = {
						oldPwd:this.oldPwd,
						newPwd:this.newPwd,
					}
					updatePassword(obj).then(res=>{
						if(res.data.status === 0){
							uni.showToast({
								title:"修改成功"
							})
							//清空本地存储的token和 userInfo
							uni.removeStorageSync("token")
							uni.removeStorageSync("userInfo")
							setTimeout(() =>{
								//修改成功跳转到login页面
								uni.reLaunch({
									url:"/pages/login/login"
								})
							},600)
						}
						else{
							uni.showToast({
								title:"修改失败"
							})
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page{
		width: 100%;
		height: 100vh;
		background-color: #F2F2F2;
		.update-form{
			width: 100%;
			height: 450rpx;
			
			margin: 0 auto;
			padding-top: 40rpx;
			.form-item{
				display: flex;
				align-items: center;
				height: 80rpx;
				line-height: 80rpx;
				padding: 20rpx;
				background-color: white;
				border-radius: 20rpx;
				margin-left: 10rpx;
				margin-bottom: 30rpx;
				input{
					margin-left: 10rpx;
				}
			}
		}
		.submit-btn{
			width: 70%;
			height: 90rpx;
			line-height: 90rpx;
			background-color: #E00724;
			margin: 450rpx auto 0rpx;
			text-align: center;
			color: white;
			border-radius: 80rpx;
			letter-spacing: 40rpx;
		}
	}
</style>
